package com.qf.mes.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import com.qf.mes.entity.MdItemType;
import com.qf.mes.mapper.MdItemTypeMapper;
import com.qf.mes.service.MdItemTypeService;
import com.qf.mes.vo.MdItemTypeVO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class MaItemTypeServiceimpl implements MdItemTypeService {
    @Resource
    private MdItemTypeMapper mdItemTypeMapper;

    @Override //使用一个sql实现多表联查
    public List<MdItemTypeVO> select(MdItemType mdItemType) {
        return mdItemTypeMapper.select(mdItemType);
    }




    @Override
    public List<Tree<String>> select3() {
        List<MdItemType> mdItemTypeList = mdItemTypeMapper.select3();
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        List<Tree<String>> treeNodes = TreeUtil.build(mdItemTypeList, "0", treeNodeConfig,
                (treeNode, tree) -> {
                    // 设置基本字段
                    tree.setId(treeNode.getItemTypeId().toString());
                    tree.setParentId(treeNode.getParentTypeId().toString());

                    // 设置额外字段
                    tree.putExtra("orderNum", treeNode.getOrderNum());
                    tree.putExtra("itemTypeName", treeNode.getItemTypeName());
                    tree.putExtra("itemTypeId", treeNode.getItemTypeId());
                    tree.putExtra("itemTypeCode", treeNode.getItemTypeCode());
                    tree.putExtra("itemOrProduct", treeNode.getItemOrProduct());
                    tree.putExtra("enableFlag", treeNode.getEnableFlag());

                    // 正确格式化并存储时间
                    if (treeNode.getCreateTime() != null) {
                        String formattedTime = format.format(treeNode.getCreateTime());
                        tree.putExtra("createTime", formattedTime);
                    } else {
                        tree.putExtra("createTime", ""); // 或设置默认值
                    }
                });
        return treeNodes;
    }
    @Override
    public void deleteMdItemType(Integer id) {
        mdItemTypeMapper.delete(id);
    }

    @Override
    public MdItemType getMdItemTypeById(Long itemTypeId) {
        return mdItemTypeMapper.getMdItemTypeById(itemTypeId);
    }

    @Override
    public void UpdateMdItemType(MdItemType mdItemType) {
        mdItemTypeMapper.updateMdItemType(mdItemType);
    }

    @Override
    public MdItemType getMdItemTypeByIdName(Integer id) {
        return mdItemTypeMapper.getMdItemTypeByIdName(id);
    }


    @Override
    public void insertMdItemType(MdItemType mdItemType) {
        mdItemType.setCreateTime(new Date(  ));
        // 获得祖先
        MdItemType parentMdItemType = mdItemTypeMapper.getMdItemTypeById(mdItemType.getParentTypeId(  ));
        String ancestors = parentMdItemType.getAncestors( );// 0,200,272
        // 拼接祖先节点
        ancestors += "," + mdItemType.getParentTypeId(  );// 0,200,272,274
        mdItemType.setAncestors(ancestors);
        // 产生新的编码,根据数据库中最后一条记录的编码,加1
        MdItemType lastMdItemType = mdItemTypeMapper.getLastMdItemType(  );
        String itemTypeCode = lastMdItemType.getItemTypeCode(  );// ITEM_TYPE_0094
        // 截取后四位,加1
        String newItemTypeCode = itemTypeCode.substring(itemTypeCode.length(  ) - 4);// 0094
        int newCode = Integer.parseInt(newItemTypeCode) + 1;// 95
        newItemTypeCode = String.format("%04d", newCode);// ITEM_TYPE_0095

        mdItemType.setItemTypeCode("ITEM_TYPE_"+newItemTypeCode);

        mdItemTypeMapper.insertMdItemType(mdItemType);
    }

}